<?php include('includes/check.php'); check_login('0');
require_once('themes/header.php');
if(isset($_POST['Character']))
{
	connectlogin();
	$query = mysql_query("SELECT * FROM `account_data` WHERE `name`='$user_name'");
	$result = mysql_fetch_array($query);
	$cash = $result['shop_points'];
	$item = $_POST['item'];
	$Character = $_POST['Character']; 
	$uniqueid = rand(100000, 200000);
	//Check Cash
	connectshop();
	$res = mysql_query("SELECT * FROM items WHERE id='$item'");
	$Row = mysql_fetch_array($res);
	$cost = $Row['price'];
	$code = $Row['code'];
	if ($cash < $cost){

	}
	elseif ($Row['type'] == 0)
	{
	echo 'Mail Sent Ingame';
	$newcash = $cash - $cost;
	connectlogin();
	mysql_query("UPDATE `account_data` SET `reward_points` = '$newcash' WHERE `name`='$user_name'");
	connectgame();
	mysql_query("INSERT INTO mail VALUES('$uniqueid','$Character','PvP Lords','Thanks','Here is you Kinah','1','null','{$Row['amount']}','null','2010-08-28 02:00:00')");
	}
	else {
	echo 'Success!';
	$newcash = $cash - $cost;
	connectlogin();
	mysql_query("UPDATE `account_data` SET `shop_points` = '$newcash' WHERE `name`='$user_name'");
	connectgame();
	mysql_query("INSERT INTO inventory VALUES('$uniqueid','{$Row['code']}','{$Row['amount']}','null','$Character','0','0','1','0','0','0','{$Row['code']}','0','0')");
}	
}
else {
if(isset($_POST['Buy'])) {
	echo "<h1>Who Gets It?</h1>";
	connectlogin();
	$query = mysql_query("SELECT * FROM `account_data` WHERE `name`='$user_name'");
	$result = mysql_fetch_array($query);
	$account = $result['id'];
	connectgame();
	$query2 = mysql_query("SELECT * FROM `players` WHERE `account_id`='$account'");
	echo '<table width="100%">';
	while($result2 = mysql_fetch_assoc($query2))
	{
	$counter++;
$background_color = ( $counter % 2 == 0 ) ? ('alt') : ('alt2');
	echo '
	<tr class="'.$background_color.'">
	<td align="center">
	<form method="POST" action="buy.php">
	<input type="hidden" name="Character" value="'.$result2['id'].'">
	<input type="hidden" name="item" value="'.$_POST['item'].'">
	<h1>'.$result2['name'].'</h1>
	</td>
	<td align="center">
	<img src="/images/'.$result2['race'].'.png">
	</td>
	<td align="center">
	<img src="/images/'.$result2['player_class'].'.png">
	</td>
	<td align="center">
	<input type="submit" class="btn_list_select" name="Give" value="Give">
	</form>
	</td>
	</tr>';
	}
	echo '</table>';
	
}
else 
{

}
}
require_once('themes/footer.php');
?>
